//
//  SEGActivityCollectViewController.m
//  ExcellentHome
//
//  Created by 揭婧雯 on 2017/8/4.
//  Copyright © 2017年 陈区. All rights reserved.
//

#import "SEGActivityCollectViewController.h"
#import "SEGActivityDetailUsersCell.h"
#import "SEGActivityCollectMyUploadViewController.h"
#import "SEGActivityCollectUploadViewController.h"

@interface SEGActivityCollectViewController ()
/**
 是否需要刷新数据
 */
@property (nonatomic, assign) BOOL shouldRefreshData;
@end

@implementation SEGActivityCollectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // 重置展示数据
    self.displayTypeArray = @[@(SEGActivityDetailCellDispalyType_Rule), @(SEGActivityDetailCellDispalyType_TencentAD), @(SEGActivityDetailCellDispalyType_JoinedUser), @(SEGActivityDetailCellDispalyType_Comment)];
    [self.tableView registerClass:[SEGActivityDetailUsersCell class] forCellReuseIdentifier:kActivityDetailUsersCellIdentifier];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if (self.shouldRefreshData) {
        //刷新评论列表
        [self requestCommentList:YES];
    }
}

#pragma mark - 根据不同类型活动进行界面设置 子类根据需要重写
/// 返回section中的行数
- (NSInteger)setRowsInSection:(NSInteger)section {
    return 1;
}

/// 返回cell
- (UITableViewCell *)setCellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SEGActivityDetailUsersCell *cell = [self.tableView dequeueReusableCellWithIdentifier:kActivityDetailUsersCellIdentifier forIndexPath:indexPath];
    [cell setUpCellWithModel:self.detailModel isOffLine:NO];
    return cell;
}

/// 设置cell高度
- (CGFloat)setHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewAutomaticDimension;
}

#pragma mark - SEGActivityRuleDelegate
- (void)myUploadbuttonAction {
    //我上传的
    SEGActivityCollectMyUploadViewController *vc = [[SEGActivityCollectMyUploadViewController alloc] initWithActId:self.actId];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - SEGActivityDetailToolBarDelegate
- (void)uploadButtonAction {
    [self.detailViewModel.joinCollectAvtivitySignal subscribeNext:^(id  _Nullable x) {
        RACTupleUnpack(NSNumber *hasUploaded, NSString *errorMessage) = x;
        if ([hasUploaded boolValue]) {
            SEGNewAlertView *alertView = [[SEGNewAlertView alloc] init];
            [alertView showWithAlertTitle:nil text:errorMessage buttonTitlesArray:@[@"取消", @"确定"] actionBlock:^(NSInteger btnIndex) {
                if (btnIndex == 1) {//确定
                    //上传作品
                    [self seg_goToUploadVC];
                }
            }];
        } else {
            //上传作品
            [self seg_goToUploadVC];
        }
    } error:^(NSError * _Nullable error) {
        if (error.code == kActicityErrorCode_NoAuth) {
            [self showNothAlterView];
            return;
        }
        [SEGAlertView showTmepMessageWithMessage:error.localizedDescription];
    }];
}

- (void)seg_goToUploadVC {
    self.shouldRefreshData = YES;
    SEGActivityCollectUploadViewController *vc = [[SEGActivityCollectUploadViewController alloc] initWithActId:self.actId isReview:[self.detailModel.isReview isEqualToString:@"2"]];
    [self.navigationController pushViewController:vc animated:YES];
}

@end
